library(effects)

#read in data
dt <- read.csv("~/Dropbox/Varieties of Unconstitutionality Rulings/JOP_replication_data.csv", header=T)
##########################
#Main Analyses
##########################
#Model 1
a <- glm(delay ~  election.ratio*chancellor_support, data=dt,family=binomial)

#Model 2
a1 <- glm(delay ~  outsupport, data=dt,family=binomial)

#Model 3
a2 <- glm(delay ~  Oral, data=dt,family=binomial)

#Model 4
a3 <- glm(delay ~  election.ratio*chancellor_support+ govt_defend +outsupport+Oral+Spending+second+pro.anti.diff+cdu+cc, data=dt,family=binomial)


#Figure 2
ef1 <-(effect("election.ratio*chancellor_support",a3, confidence.level=.9,x.var="chancellor_support",xlevels=list("election.ratio"=c(0.18,0.875),"chancellor_support"=c(20:55))))

ef1 <-data.frame(ef1)
 ef2 <- subset(ef1,election.ratio==0.875)

plot(ef2$fit~ef2$chancellor_support,type="l",ylim=c(0,0.98),lwd=2,xlab="Support for Chancellor's Party",ylab="Probability of Incompatibility Ruling",col="black",axes=F)
lines(ef2$lower ~ef2$chancellor_support,type="l",lty=2,lwd=2,col="black")
lines(ef2$upper ~ef2$chancellor_support,type="l",lty=2,lwd=2,col="black")
axis(1)
axis(2)
box(bty="L")

#Figure 3
par(mfrow=c(1,2))
par(mar=c(2.75,5,2.5,0.5))

ef1 <-(effect("outsupport",a3, confidence.level=.9,x.var="outsupport"))
ef1 <- data.frame(ef1)
 ef2 <- subset(ef1,outsupport==1)
ef3 <-  subset(ef1,outsupport==0)

x <- c(0:1)
y<- c(0:1)

plot(x,y,type="n", xlim=c(0:1), ylim=c(0.2,.9), axes=F,tck=0, xlab="", ylab="Probability of Incompatibility Ruling", xaxs="i", yaxs="i",cex.lab=1.25)


points(.3,ef3$fit, pch=16,col="black")
segments(.3, ef3$lower, .3, ef3$upper, lty=1, lwd=2, col="black")
segments(.28,ef3$lower,.32,ef3$lower, lty=1, lwd=2, col="black")
segments(.28,ef3$upper,.32,ef3$upper, lty=1, lwd=2, col="black")

points(.7,ef2$fit, pch=16 , col="black")
segments(.7, ef2$lower,.7,ef2$upper, lty=1, lwd=2, col="black")
segments(.68,ef2$upper,.72,ef2$upper, lty=1, lwd=2, col="black")
segments(.68,ef2$lower,.72,ef2$lower, lty=1, lwd=2, col="black")

z <- c(.3,.4,.5,.6,.7,.8,.9)
axis(2, at=z, tck=-0.02, labels=expression(0.3,0.4,0.5,0.6,0.7,0.8,0.9))
axis(1, at=x, labels=expression(,), tck=0)
w <- c(.3,.7)
mtext("No Third Party\nSupport", side=1, line=1.5, at=0.3)

mtext("With Third Party\nSupport", side=1, line=1.5, at=0.7)
box()


ef1 <-(effect("Oral",a3, confidence.level=.9,x.var="Oral"))
ef1 <- data.frame(ef1)
 ef2 <- subset(ef1, Oral ==1)
ef3 <-  subset(ef1, Oral ==0)

x <- c(0:1)
y<- c(0:1)
par(mar=c(2.75,0,2.5,5))
plot(x,y,type="n", xlim=c(0:1), ylim=c(0.2,.9), axes=F,tck=0, xlab="", ylab="", xaxs="i", yaxs="i")


points(.3,ef3$fit, pch=16,col="black")
segments(.3, ef3$lower, .3, ef3$upper, lty=1, lwd=2, col="black")
segments(.28,ef3$lower,.32,ef3$lower, lty=1, lwd=2, col="black")
segments(.28,ef3$upper,.32,ef3$upper, lty=1, lwd=2, col="black")

points(.7,ef2$fit, pch=16 , col="black")
segments(.7, ef2$lower,.7,ef2$upper, lty=1, lwd=2, col="black")
segments(.68,ef2$upper,.72,ef2$upper, lty=1, lwd=2, col="black")
segments(.68,ef2$lower,.72,ef2$lower, lty=1, lwd=2, col="black")

axis(1, at=x, labels=expression(,), tck=0)
w <- c(.3,.7)
mtext("No Public Oral\nHearing", side=1, line=1.5, at=0.3)

mtext("With Public Oral\nHearing", side=1, line=1.5, at=0.7)

box()

##########################
#Supplementary Analyses
##########################
#Descriptive statistics calculated using "summary" and "sd" functions for each variable

#Histograms

hist(dt$election.ratio, breaks=10, main="",xlab="Election Cycle", ylab="Number of Cases")
box()

hist(dt$chancellor_support, breaks=8, main="",xlab="Government Support", ylab="Number of Cases")
box()

#Robustness: Include controls in original analyses but not other IVs
robust1 <- glm(delay ~  election.ratio*chancellor_support+Spending+second+pro.anti.diff+ govt_defend +cdu+cc, data=dt,family=binomial)
summary(robust1)

#six months robustness
robust2 <- glm(delay ~  next.election.six.months*chancellor_support+Spending+second+pro.anti.diff+ govt_defend +cdu+cc, data=dt,family=binomial)
summary(robust2)

robust3 <- glm(delay ~  next.election.six.months*chancellor_support+outsupport+Oral+Spending+second+pro.anti.diff+ govt_defend +cdu+cc, data=dt,family=binomial)
summary(robust3)

